from pyquery import PyQuery as pq

html='''
    <div>
    <ul>
    <li class="item-0">first item</li>
    <li class="item-1"><a href="link2.html">second item</a></li>
    <li class="item-0"><a href="link3.html"><span class="bold">third item</span></a></li>
    <li class="item-1"><a href="link4.html">fourth item</a></li>
    <li class="item-0"><a href="link5.html">fifth item</a></li>
    </ul>
    </div>
'''
doc=pq(html)
print(doc('li'))

doc=pq(url='https://cuiqingcai.com')
print(doc('title'))

html='''
    <div id="container">
    <ul class="list">
    <li class="item-0">first item</li>
    <li class="item-1"><a href="link2.html">second item</a></li>
    <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
    <li class="item-1 active"><a href="link4.html">fourth item</a></li>
    <li class="item-0"><a href="link5.html">fifth item</a></li>
    </ul>
    </div>
'''

doc=pq(html)
print(doc('#container .list li'))
print(type(doc('#container .list li')))

'''
查询子节点
'''

doc=pq(html)
items=doc('.list')
print(type(items))
print(items)
lis=items.find('li')
print(type(lis))
print(lis)

lis=items.children()
print(type(lis))
print(lis)

print(items.children('.active'))

html='''
    <div class="wrap">
    <div id="container">
    <ul class="list">
    <li class="item-0">first item</li>
    <li class="item-1"><a href="link2.html">second item</a></li>
    <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
    <li class="item-1 active"><a href="link4.html">fourth item</a></li>
    <li class="item-0"><a href="link5.html">fifth item</a></li>
    </ul>
    </div>
    </div>
'''

'''
查询父节点
'''
doc=pq(html)
items=doc('.list')
container=items.parent()
print(type(container))
print(container)

parents=items.parents()
print(type(parents))
print(parents)

parents=items.parents('.wrap')
print(parents)

'''
查询兄弟节点
'''
li=doc('.list .item-0.active')
print(li.siblings())
print(li.siblings('.active'))

'''
遍历
'''
lis=doc('li').items()
print(type(lis))
for li in lis:
    print(li,type(li))
